<template>
  <el-form
    ref="zhuceUserRef"
    :model="zhuceUser"
    :rules="zhuceRules"
    label-width="120px"
    class="registerForm sign-up-form"
  >
    <el-form-item label="用户名" prop="name">
      <el-input v-model="zhuceUser.name" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input
        v-model="zhuceUser.email"
        placeholder="请输入 Email..."
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="zhuceUser.password"
        type="password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="password2">
      <el-input
        v-model="zhuceUser.password2"
        type="password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>

    <el-form-item label="选择身份">
      <el-select v-model="zhuceUser.role" placeholder="请选择身份">
        <el-option label="管理员" value="admin"></el-option>
        <el-option label="用户" value="user"></el-option>
        <el-option label="游客" value="visitor"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="handlezhuce(zhuceUserRef)"
        class="submit-btn"
        >注册</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { defineComponent,getCurrentInstance, ref } from "vue";
import type { ElForm } from "element-plus";
import {useRouter} from 'vue-router'
export default defineComponent({
  name: "",
  props: {
  zhuceUser:{
    type:Object,
    required:true
  },zhuceRules:{
    type:Object,
    required:true
  }},
  setup(props,ctx) {
    type FormInstance = InstanceType<typeof ElForm>; //  ElForm,来自element-plus，官方文档中这样用
    const zhuceUserRef = ref<FormInstance>();
    // @ts-ignore
    const { proxy } = getCurrentInstance();
    const router = useRouter()
    // 注册事件
    const handlezhuce =(formEl: FormInstance | undefined)=>{
      router.push("/home")

      if (!formEl) return
      formEl.validate((valid) => {
        if (valid) {
          // @ts-ignore
          proxy.$axios.post(
            "/api/v1/auth/register",
            props.zhuceUser)
            .then((res:any)=>{
            // 注册成功
            if(res.success){
              console.log("注册成功");
              
            }
            console.log(222);
            router.push("/home")
          })
        } else {
          console.log('error submit!')
          return false 
        }
      })
    }
    return {
      zhuceUserRef,
      handlezhuce,
    }
  },
});
</script>

<style>
</style>